<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getRank } from "@/api";
export default {
  data() {
    return {
      echartsDom: null,
      rankData: null,
      startNum: 0,
      endNum: 9,
      timeId: null,
    };
  },
  mounted() {
    this.getData();
    this.initEachat();
    this.screenAdapter();
  },
  computed: {
    ...mapState(["style"]),
  },
  watch: {
    style() {
      console.log("主题切换了");
      this.echartsDom.dispose(); // 销毁当前的图表
      this.initEachat(); // 重新以最新的主题名称初始化图表对象
      this.screenAdapter(); // 完成屏幕的适配
      this.upData(); // 更新图表的展示
    },
  },
  methods: {
    // 获取数据
    async getData() {
      let data = await getRank();
      this.rankData = data.sort((a, b) => b.value - a.value);
      this.upData();
      this.startIntVal();
    },
    // 初始化Eachats元素
    initEachat() {
      this.echartsDom = this.$echarts.init(this.$refs.rank_ref, this.style); //第一个参数是dom元素，第二个是主题
      const initOption = {
        title: {
          text: "▎地区销售排行",
          left: 20,
          top: 20,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            z: 0,
            lineStyle: {
              color: "#2D3443",
              type: "solid",
            },
          },
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true, // 距离是包含坐标轴上的文字
        },
        series: [
          {
            label: {
              show: true,
              position: "top",
              textStyle: {
                color: "#fff",
              },
            },
            type: "bar",
          },
        ],
      };
      this.echartsDom.setOption(initOption);
      this.echartsDom.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.echartsDom.on("mouseout", () => {
        this.startIntVal();
      });
    },
    // 浏览器大小变化
    screenAdapter() {
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
      let adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            },
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
            },
          },
        ],
      };
      this.echartsDom.setOption(adapterOption);
      this.echartsDom.resize();
    },
    // 更新页面元素
    upData() {
      let xDataName = this.rankData.map((item) => item.name);
      let yData = this.rankData.map((item) => item.value);
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"],
      ];
      const initOption = {
        xAxis: {
          data: xDataName,
        },
        dataZoom: [
          {
            show: false,
            startValue: this.startNum,
            endValue: this.endNum,
          },
        ],
        series: [
          {
            data: yData,
            itemStyle: {
              color: (arg) => {
                let targetColorArr = null;
                if (arg.value > 300) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0],
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1],
                  },
                ]);
              },
            },
          },
        ],
      };
      this.echartsDom.setOption(initOption);
    },
    // 横向滚动
    startIntVal() {
      if (this.timeId) clearInterval(this.timeId);
      this.timeId = setInterval(() => {
        this.startNum++;
        this.endNum++;
        if (this.endNum > this.rankData.length) {
          this.startNum = 0;
          this.endNum = 9;
        }
        this.upData();
      }, 2000);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>